<?php
include APPLICATION_PATH."/models/Area_Province_Category.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Admin_AreaprovincecategoryController extends Zend_Controller_Action{
    public function init(){
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->model= new Model_Area_Province_Category();
        $this->modelMapper = new Model_Area_Province_CategoryMapper();
    }
    public function addAction(){
        if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."admin/area/index";            
            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }
            if(strlen($_POST["area_id"])>0)
            {
                $this->model->setArea_Id($_POST["area_id"]);
            } 
            if(isset($_POST["province_id"])){
                $this->model->setProvince_Id($_POST["province_id"]);
            }
            if(isset($_POST["order"])){
                $this->model->setOrder($_POST["order"]);
            }
            if(isset($_POST["status"])){
                $status=1;
            } else {
                $status=0;
            }
            $this->model->setStatus($status);
             $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }     
           $this->view->item= $this->model;
    }
    public function editAction(){
        $id = $this->_getParam("id","");        
        $this->view->itemm=$id;
        $redirectUrl=$this->aConfig["site"]["url"]."admin/area/index";
        if(empty($id)){
            $this->_redirect($redirectUrl);
        }
        $this->modelMapper->find($id,$this->model);
        $getId=$this->model->getId();
        if(empty($getId)){
            $this->_redirect($redirectUrl);
        }
        if($this->getRequest()->isPost()){
            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }
            if(strlen($_POST["area_id"])>0){
                $this->model->setArea_Id($_POST["area_id"]);
            }
            if(strlen($_POST["province_id"])>0){
                $this->model->setProvince_Id($_POST["province_id"]);
            }
            if(isset($_POST["order"])){
                $this->model->setOrder($_POST["order"]);
            }
            if(isset($_POST["status"])){
                $status_area=1;
            } else {
                $status_area=0;
            }
            $this->model->setStatus($status_area);             
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);           
        }
        $this->view->item=$this->model;
    }
    public function deleteAction(){
        $id= $this->_getParam("id","");
        $redirectUrl=$this->aConfig["site"]["url"]."admin/area/index";               
        $this->modelMapper->delete($id);
        $this->_redirect($redirectUrl);
    }
}